<?php 
namespace app\admin\controller;

use think\Controller;
use think\facade\Validate;

/**
 * 登录
 */
class Login extends Controller
{
	/**
	 * 登录页面
	 */
	public function index()
	{
		return view();
	}

	/**
	 * 执行登录
	 */
	public function login()
	{
		$data = [
			'username' => input('username'),
			'password' => input('password')
		];

		$rule = [
			'username' => 'require',
			'password' => 'require'
		];

		$msg = [
			'username.require' => '请输入用户名',
			'password.require' => '请输入密码',
		];

		$validate = Validate::make($rule,$msg);
		if(!$validate->check($data)){
			$this->result('', 0, $validate->getError(), 'json');
		}else{
			$admininfo = model('Admin')::getByUsername($data['username']);
			if($admininfo){
				if($admininfo['password'] != md5($data['password'])){
					$this->result('',0,'密码不正确！','json');
				}
				if($admininfo['status'] != 1){
					$this->result('',0,'此账号已经被禁用','json');
				}
				cookie('admin_id',$admininfo['id'],180*24*60*60);
				cookie('username',$admininfo['username'],180*24*60*60);

				$this->result('',1,'登录成功','json');
			}else{
				$this->result('',0,'账号不存在！','json');
			}
		}
	}

	/*退出*/
	public function loginout()
	{
		cookie('admin_id',null);
		cookie('username',null);

		$this->result('',1,'成功退出','json');
	}
}

 ?>